package com.monitor.task.service.impl;

import com.monitor.listenter.NodeRegisterService;
import com.monitor.pojo.MonitorInfo;
import com.monitor.service.MonitorService;
import com.monitor.task.email.EmailInfo;
import com.monitor.task.email.EmailTemplate;
import com.monitor.task.email.SendEmail;
import com.monitor.task.service.MonitorTask;
import com.monitor.task.util.MonitorTaskUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName MonitorTaskNowImpl
 * @Package com.monitor.task.service.impl
 * @Description 当日查询报警
 * @Author zhixingbai
 * @Create 2018/10/1712:58
 */
@Service("taskCommon")
public class MonitorTaskNowImpl extends MonitorTask {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MonitorService monitorService;
    @Autowired
    private EmailInfo emailInfo;
    @Autowired
    private NodeRegisterService nodeRegisterService;

    @Override
    public void taskWaring() {
        logger.info("定时任务开始启动");
        String ids=nodeRegisterService.getMachineAllotData();
        List<MonitorInfo> datas = monitorService.selectMonitors(ids);
        StringBuffer bodyContent = new StringBuffer();
        long startTime = System.currentTimeMillis();
        logger.info("账户总数：{}", datas.size());
        int sendCount = 0;
        for (MonitorInfo data : datas) {
            boolean sendFlag = MonitorTaskUtils.warning(data, bodyContent);
            if (!sendFlag) {
                sendCount++;
            }
        }
        logger.info("报警账户数：{}", sendCount);
        StringBuffer mailContent = new StringBuffer();
        mailContent.append(String.format(EmailTemplate.TEMPLATE_CONTENT, datas.size(), sendCount))
                .append(EmailTemplate.TEMPLATE_HEADER)
                .append(bodyContent.toString())
                .append(EmailTemplate.TEMPLATE_END);
        logger.info("时间对比规则，总耗时：{}", System.currentTimeMillis() - startTime);
        //邮件内容，发送邮件
        if (sendCount > 0) {
             SendEmail.sendEmail(mailContent.toString(), emailInfo.getSubject(),
                    emailInfo.getUrl());
            //入库
            //saveSendMail(mailContent.toString(), result);
        }
    }
}
